package com.dgut.courses_selecting_sys.rabbitmq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dgut.courses_selecting_sys.constants.CourseStatus;
import com.dgut.courses_selecting_sys.constants.RabbitMqKey;
import com.dgut.courses_selecting_sys.constants.RedisKey;
import com.dgut.courses_selecting_sys.entity.Course;
import com.dgut.courses_selecting_sys.mapper.CourseMapper;
import com.dgut.courses_selecting_sys.utils.RedisUtil;
import com.dgut.courses_selecting_sys.utils.ShiroUtils;
import com.dgut.courses_selecting_sys.utils.core.StrFormatter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Component
@RabbitListener(queuesToDeclare = @Queue(value = RabbitMqKey.COURSE_TO_CHOICE_DELAY_PROCESS_QUEUE,durable = "true"))
public class CourseToChoiceConsumer {

    @Autowired
    CourseMapper courseMapper;
    @Autowired
    RedisUtil redisUtil;

    @RabbitHandler
    public void changeToChoiceStatus(String message) throws Exception {
        Integer id=Integer.valueOf(message);
        Course courseById = courseMapper.selectById(id);
        if (courseById != null && courseById.getCount() != null) {
            Long time=(courseById.getEndChooseTime().getTime()-System.currentTimeMillis())/1000;
            redisUtil.set(RedisKey.COURSE_COUNT.concat(message), courseById.getCount(), time);
            Course courseDb = new Course();
            courseDb.setStatus(String.valueOf(CourseStatus.CHOICE));
            courseDb.setCourseId(courseById.getCourseId());
            courseMapper.updateById(courseDb);
            log.info("课程开选通知:"+courseDb);
        }
    }

}
